<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="100px">
      <el-form-item label="区域代码" prop="XZQYDM">
        <el-input
          v-model="queryParams.XZQYDM"
          placeholder="请输入行政区域代码"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="区域名称" prop="XZQYMC">
        <el-input
          v-model="queryParams.XZQYMC"
          placeholder="请输入行政区域名称"
          clearable
          @keyup.enter="handleQuery"
        />
      </el-form-item>
      <el-form-item label="层级" prop="ZSCJ">
        <el-select v-model="queryParams.ZSCJ" placeholder="请选择层级" clearable>
          <el-option
            v-for="dict in xzcj"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
        <el-button icon="Refresh" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['manage:zcps3:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['manage:zcps3:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['manage:zcps3:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['manage:zcps3:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="zcps3List" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="年代" align="center" prop="nd" />
      <el-table-column label="行政区域代码" align="center" prop="xzqydm" />
      <el-table-column label="行政区域名称" align="center" prop="xzqymc" show-overflow-tooltip />
      <el-table-column label="层级" align="center" prop="zxcj">
        <template #default="scope">
          <dict-tag :options="xzcj" :value="scope.row.zscj"/>
        </template>
      </el-table-column>
      <el-table-column label="所属区域代码" align="center" prop="fxzqydm" />
      <el-table-column label="ZG1人数" align="center" prop="zgrs1" />
      <el-table-column label="FG1人数" align="center" prop="fgrs1" />
      <el-table-column label="ZJ1人数" align="center" prop="zjrs1" />
      <el-table-column label="CJ1人数" align="center" prop="cjrs1" />
      <el-table-column label="ZG2人数" align="center" prop="zgrs2" />
      <el-table-column label="FG2人数" align="center" prop="fgrs2" />
      <el-table-column label="ZJ2人数" align="center" prop="zjrs2" />
      <el-table-column label="CJ2人数" align="center" prop="cjrs2" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" @click="handleDelete(scope.row)" v-hasPermi="['manage:zcps3:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改数据表三对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="zcps3Ref" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="年代" prop="ND">
          <el-input v-model="form.ND" placeholder="请输入年代" />
        </el-form-item>
        <el-form-item label="行政区域代码" prop="XZQYDM">
          <el-input v-model="form.XZQYDM" placeholder="请输入行政区域代码" />
        </el-form-item>
        <el-form-item label="行政区域名称" prop="XZQYMC">
          <el-input v-model="form.XZQYMC" placeholder="请输入行政区域名称" />
        </el-form-item>
        <el-form-item label="${comment}" prop="TJDWF">
          <el-input v-model="form.TJDWF" placeholder="请输入${comment}" />
        </el-form-item>
        <el-form-item label="层级" prop="ZSCJ">
          <el-select v-model="form.ZSCJ" placeholder="请选择层级">
            <el-option
              v-for="dict in xzcj"
              :key="dict.value"
              :label="dict.label"
              :value="parseInt(dict.value)"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="所属区域代码" prop="FXZQYDM">
          <el-input v-model="form.FXZQYDM" placeholder="请输入所属区域代码" />
        </el-form-item>
        <el-form-item label="ZG1人数" prop="ZGRS1">
          <el-input v-model="form.ZGRS1" placeholder="请输入ZG1人数" />
        </el-form-item>
        <el-form-item label="FG1人数" prop="FGRS1">
          <el-input v-model="form.FGRS1" placeholder="请输入FG1人数" />
        </el-form-item>
        <el-form-item label="ZJ1人数" prop="ZJRS1">
          <el-input v-model="form.ZJRS1" placeholder="请输入ZJ1人数" />
        </el-form-item>
        <el-form-item label="CJ1人数" prop="CJRS1">
          <el-input v-model="form.CJRS1" placeholder="请输入CJ1人数" />
        </el-form-item>
        <el-form-item label="ZG2人数" prop="ZGRS2">
          <el-input v-model="form.ZGRS2" placeholder="请输入ZG2人数" />
        </el-form-item>
        <el-form-item label="FG2人数" prop="FGRS2">
          <el-input v-model="form.FGRS2" placeholder="请输入FG2人数" />
        </el-form-item>
        <el-form-item label="ZJ2人数" prop="ZJRS2">
          <el-input v-model="form.ZJRS2" placeholder="请输入ZJ2人数" />
        </el-form-item>
        <el-form-item label="CJ2人数" prop="CJRS2">
          <el-input v-model="form.CJRS2" placeholder="请输入CJ2人数" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Zcps3">
import { listZcps3, getZcps3, delZcps3, addZcps3, updateZcps3 } from "@/api/manage/zcps3";

const { proxy } = getCurrentInstance();
const { xzcj } = proxy.useDict('xzcj');

const zcps3List = ref([]);
const open = ref(false);
const loading = ref(true);
const showSearch = ref(true);
const ids = ref([]);
const single = ref(true);
const multiple = ref(true);
const total = ref(0);
const title = ref("");

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    XZQYDM: null,
    XZQYMC: null,
    ZSCJ: null,
    FXZQYDM: null,
  },
  rules: {
  }
});

const { queryParams, form, rules } = toRefs(data);

/** 查询数据表三列表 */
function getList() {
  loading.value = true;
  listZcps3(queryParams.value).then(response => {
    zcps3List.value = response.rows;
    total.value = response.total;
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false;
  reset();
}

// 表单重置
function reset() {
  form.value = {
    ND: null,
    XZQYDM: null,
    XZQYMC: null,
    TJDWF: null,
    ZSCJ: null,
    FXZQYDM: null,
    ZGRS1: null,
    FGRS1: null,
    ZJRS1: null,
    CJRS1: null,
    ZGRS2: null,
    FGRS2: null,
    ZJRS2: null,
    CJRS2: null
  };
  proxy.resetForm("zcps3Ref");
}

/** 搜索按钮操作 */
function handleQuery() {
  queryParams.value.pageNum = 1;
  getList();
}

/** 重置按钮操作 */
function resetQuery() {
  proxy.resetForm("queryRef");
  handleQuery();
}

// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.ND);
  single.value = selection.length != 1;
  multiple.value = !selection.length;
}

/** 新增按钮操作 */
function handleAdd() {
  reset();
  open.value = true;
  title.value = "添加数据表三";
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset();
  const _ND = row.ND || ids.value
  getZcps3(_ND).then(response => {
    form.value = response.data;
    open.value = true;
    title.value = "修改数据表三";
  });
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["zcps3Ref"].validate(valid => {
    if (valid) {
      if (form.value.ND != null) {
        updateZcps3(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功");
          open.value = false;
          getList();
        });
      } else {
        addZcps3(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功");
          open.value = false;
          getList();
        });
      }
    }
  });
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _NDs = row.ND || ids.value;
  proxy.$modal.confirm('是否确认删除数据表三编号为"' + _NDs + '"的数据项？').then(function() {
    return delZcps3(_NDs);
  }).then(() => {
    getList();
    proxy.$modal.msgSuccess("删除成功");
  }).catch(() => {});
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('manage/zcps3/export', {
    ...queryParams.value
  }, `zcps3_${new Date().getTime()}.xlsx`)
}

getList();
</script>
